import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:blog_chyk/routes/app_routes.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    // 存储当前选中的索引，初始化为文章列表
    final RxInt currentIndex = 0.obs;

    // 定义路由与索引的映射关系
    final List<String> routeList = [
      AppRoutes.articleList,
      AppRoutes.aboutMe,
      AppRoutes.setting,
    ];

    // 初始化时跳转到第一个路由
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Get.toNamed(routeList[0]);
    });

    return Scaffold(
      // 路由视图容器（对应Vue的<router-view>）
      body: GetRouterOutlet(initialRoute: AppRoutes.home),
      bottomNavigationBar: SafeArea(
        child: Obx(
          () => BottomNavigationBar(
            currentIndex: currentIndex.value,
            // 点击底部导航项切换路由
            onTap: (index) {
              currentIndex.value = index;
              Get.toNamed(routeList[index]);
            },
            items: const [
              BottomNavigationBarItem(icon: Icon(Icons.article), label: '文章列表'),
              BottomNavigationBarItem(icon: Icon(Icons.person), label: '关于我'),
              BottomNavigationBarItem(icon: Icon(Icons.settings), label: '设置'),
            ],
          ),
        ),
      ),
    );
  }
}
